package com.cloud.school.bus.teacherhelper.modules.stuclass;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.os.Bundle;
import android.os.Debug;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshListView;
import com.android.support.jhf.androidpulltorefresh.library.PullToRefreshBase.Mode;
import com.android.support.jhf.debug.DebugLog;
import com.cloud.school.bus.teacherhelper.R;
import com.cloud.school.bus.teacherhelper.base.slidingfragment.BaseAboveFragment;
import com.cloud.school.bus.teacherhelper.base.view.TitleNavBarView;
import com.cloud.school.bus.teacherhelper.entitys.Student;
import com.cloud.school.bus.teacherhelper.modules.SlidingActivity;
import com.cloud.school.bus.teacherhelper.modules.home.HomeFragment;
import com.cloud.school.bus.teacherhelper.modules.stuclass.adapter.StudentAdapter;
import com.cloud.school.bus.teacherhelper.modules.uploadlist.uploadutils.UploadFileUtils;

/**
 * 学生列表
 * 
 * @author jiahongfei
 * 
 */
public class StudentListFragment extends BaseAboveFragment {

	private PullToRefreshListView mPullToRefreshListView;
	private StudentAdapter mStudentAdapter;
	private TextView mStuCountTextView;
	private ViewGroup mBottomLayout;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		super.onCreateView(inflater, container, savedInstanceState);
		View view = inflater.inflate(R.layout.activity_student, null);
		setViewData(view);
		return view;
	}

	@Override
	protected void setViewData(View view) {

		mBottomLayout = (ViewGroup) view.findViewById(R.id.bottomLayout);
		mStuCountTextView = (TextView) view.findViewById(R.id.stuCountTextView);

		mPullToRefreshListView = (PullToRefreshListView) view
				.findViewById(R.id.listView);
		mPullToRefreshListView.setMode(Mode.DISABLED);

		setListener(view);

		setTitleNavBar(view);

		((SlidingActivity) mParentContext).getClassinfo(new Runnable() {

			@Override
			public void run() {
				mStudentAdapter = new StudentAdapter(mParentContext,
						mApplication.mStudentList);
				mPullToRefreshListView.setAdapter(mStudentAdapter);
				updateStudentCount();
			}
		});

	}
	
	private void updateStudentCount() {
		if (null == mApplication.mStudentList
				|| 0 == mApplication.mStudentList.size()) {
			mBottomLayout.setVisibility(View.GONE);
		} else {
			mBottomLayout.setVisibility(View.VISIBLE);
			mStuCountTextView.setText(getString(R.string.students_x)
					+ " " + mApplication.mStudentList.size());
		}
	}

	@Override
	protected void setListener(View view) {
		mPullToRefreshListView
				.setOnItemClickListener(new OnItemClickListener() {

					@Override
					public void onItemClick(AdapterView<?> arg0, View arg1,
							int arg2, long arg3) {
						Intent intent = new Intent(mParentContext,
								StudentDetailActivity.class);
						intent.putExtra("student",
								mApplication.mStudentList.get(arg2));
						intent.putExtra("index", arg2);
						startActivityForResult(intent, 1);
					}
				});
	}

	@Override
	protected void setTitleNavBar(View view) {
		TitleNavBarView titleNavBarView = (TitleNavBarView) view
				.findViewById(R.id.titleNavBarView);
		titleNavBarView.setMessage(getString(R.string.student));
		titleNavBarView.setLeftButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				((SlidingActivity) mParentContext).toggle();
			}
		});
		titleNavBarView.setRightButtonImageString(R.drawable.btn_send_xml,
				getString(R.string.add), android.R.color.white);
		titleNavBarView.setRightButtonOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent(mParentContext,
						AddStudentNamePhoneActivity.class);
				startActivityForResult(intent, 0);
			}
		});
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case 0: {
			// 添加成功返回
			switch (resultCode) {
			case Activity.RESULT_OK: {
				mStudentAdapter.notifyDataSetChanged();
				updateStudentCount();
				break;
			}
			case Activity.RESULT_CANCELED: {
				break;
			}
			default:
				break;
			}
			break;
		}
		case 1: {
			// 学生详情返回，刷新对应的列表item
			switch (resultCode) {
			case Activity.RESULT_OK: {
				if (null != data) {
					Student student = (Student) data
							.getSerializableExtra("student");
					int index = data.getIntExtra("index", 0);
					if (null != student) {
						Student removeStudent = mApplication.mStudentList
								.remove(index);
						mApplication.mStudentList.add(index, student);
					}
				}
				mStudentAdapter.notifyDataSetChanged();
				updateStudentCount();
				break;
			}
			case Activity.RESULT_CANCELED: {
				break;
			}
			default:
				break;
			}
		}
		default:
			break;
		}
	}
	
	/**
	 * 判断网络连接监听
	 */
	private BroadcastReceiver mNetConnectBroadcastReceiver = new BroadcastReceiver() {
		
		@Override
		public void onReceive(Context context, Intent intent) {
			if (ConnectivityManager.CONNECTIVITY_ACTION.equals(intent.getAction())) {
				UploadFileUtils.getUploadUtils().setContext(mParentContext);
				UploadFileUtils.getUploadUtils().setFragment(mFragment);
				UploadFileUtils.getUploadUtils().uploadFileService();
			}
		}
	};

	@Override
	public void onResume() {
		super.onResume();
		// 注册广播
		IntentFilter netConnectIntentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
		mParentContext.registerReceiver(mNetConnectBroadcastReceiver, netConnectIntentFilter);
	}

	@Override
	public void onPause() {
		super.onPause();
		mParentContext.unregisterReceiver(mNetConnectBroadcastReceiver);
	}

}
